package io.kiki.sba.registry.common.model.slot.func;

import io.kiki.sba.registry.util.MessageDigests;

import java.nio.charset.StandardCharsets;


public class MD5HashFunction implements HashFunction {


    public MD5HashFunction() {
    }


    @Override
    public int hash(Object object) {
        byte[] bytes = MessageDigests.md5().digest(object.toString().getBytes(StandardCharsets.UTF_8));
        // HACK just take the first 4 digits and make it an integer.
        // apparently this is what other algorithms use to turn it into an int
        // value.
        int h0 = (bytes[0] & 0xFF);
        int h1 = (bytes[1] & 0xFF) << 8;
        int h2 = (bytes[2] & 0xFF) << 16;
        int h3 = (bytes[3] & 0xFF) << 24;
        return h0 + h1 + h2 + h3;
    }
}
